import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:stage_app/constants/color_constant.dart';
import 'package:stage_app/utils/util.dart';
import 'package:stage_app/wigets/button/captcha_button.dart';
import 'package:flutter/services.dart';

class LoginItem extends StatefulWidget {
  final IconData prefixIcon;
  final bool hasSuffixIcon;
  final String labelText;
  final String hintText;
  final int maxLength;
  final TextInputType keyboardType;
  final String type; // 类型 text/password/captcha
  final TextEditingController controller;

  const LoginItem(
      {Key key,
      this.type,
      this.prefixIcon,
      this.hasSuffixIcon = false,
      this.labelText,
      this.maxLength,
      this.keyboardType,
      this.hintText,
      this.controller})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _LoginItemState();
}

class _LoginItemState extends State<LoginItem> {
  bool _obscureText;

  @override
  void initState() {
    super.initState();
    _obscureText = widget.hasSuffixIcon;
  }

  @override
  Widget build(BuildContext context) {
    List widgets = new List<Widget>();
    if (widget.prefixIcon != null) {
      widgets.add(_buildIconButton());
    }
    widgets.add(_buildExpanded());
    return new Row(
      children: widgets,
    );
  }

  IconButton _buildIconButton() {
    return new IconButton(
        icon: new Icon(
          widget.prefixIcon,
          color: Theme.of(context).primaryColor,
        ),
        onPressed: null);
  }

  Expanded _buildExpanded() {
    return new Expanded(child: _getTextField());
  }

  getSuffixIcon() {
    if (widget.type == 'password') {
      return new IconButton(
          icon: new Icon(
            _obscureText ? Icons.visibility : Icons.visibility_off,
            color: ColorConstant.gray_66,
          ),
          onPressed: () {
            setState(() {
              _obscureText = !_obscureText;
            });
          });
    }
    return null;
  }

  Widget _getTextField() {
    if (widget.type == 'captcha') {
      return _getCaptchaTextField();
    }
    return _getCommonTextField();
  }

  Widget _getCommonTextField() {
    return new TextField(
      obscureText: _obscureText,
      controller: widget.controller,
      style: new TextStyle(color: ColorConstant.gray_66, fontSize: 15),
      keyboardType: widget.keyboardType,
      inputFormatters:  <TextInputFormatter>[
        LengthLimitingTextInputFormatter(widget.maxLength)//限制长度
      ],
      decoration: new InputDecoration(
        labelText: widget.labelText,
        labelStyle: new TextStyle(color: ColorConstant.gray_99, fontSize: 15),
        contentPadding: EdgeInsets.fromLTRB(24.0, 10.0, 24.0, 10.0),
        hintText: widget.hintText,
        hintStyle: new TextStyle(color: ColorConstant.gray_99, fontSize: 15),
        suffixIcon: getSuffixIcon(),
        focusedBorder: new OutlineInputBorder(
            borderRadius: BorderRadius.circular(22.0),
            borderSide: new BorderSide(color: ColorConstant.app_main)),
        enabledBorder: new OutlineInputBorder(
            borderRadius: BorderRadius.circular(22.0),
            borderSide: new BorderSide(color: ColorConstant.gray_99)),
      ),
    );
  }

  Widget _getCaptchaTextField() {
    return new Stack(
      alignment: Alignment.centerRight,
      children: <Widget>[
        _getCommonTextField(),
        new Padding(
          padding: EdgeInsets.only(left: 26, right: 26),
          child: new CaptchaButton(
              textColor: ColorConstant.app_main,
              getVCode: () {
                Util.showSnackBar(context, '发送验证码');
              }),
        )
      ],
    );
  }
}
